import 'dart:io';
import 'package:get/get.dart';
import 'package:oramemo/entity/conversation_list_entity.dart';
import 'package:oramemo/entity/upload_voice_entity.dart';
import 'package:oramemo/entity/voice_record_detail_entity.dart';
import 'package:oramemo/entity/voice_record_entity.dart';
import '../repositories/record_repository.dart';
import '../models/call_record.dart';
import '../models/voice_record.dart';

class RecordController extends GetxController {
  final RecordRepository _repository = RecordRepository();
  final _isLoading = false.obs;
  final RxList<ConversationListRows> _callRecords = <ConversationListRows>[].obs;
  final RxList<VoiceRecordRows> _voiceRecords = <VoiceRecordRows>[].obs;
  final Rx<VoiceRecordDetailData?> _selectedVoiceRecord = Rx<VoiceRecordDetailData?>(null);

  bool get isLoading => _isLoading.value;

  List<ConversationListRows> get callRecords => _callRecords;

  List<VoiceRecordRows> get voiceRecords => _voiceRecords;

  VoiceRecordDetailData? get selectedVoiceRecord => _selectedVoiceRecord.value;

  Future<void> fetchCallRecords({
    required int page,
    int size = 10,
    String? deviceId,
    DateTime? startTime,
    DateTime? endTime,
  }) async {
    try {
      _isLoading.value = true;
      final response = await _repository.getCallRecords(
        page: page,
        size: size,
        deviceId: deviceId,
        startTime: startTime,
        endTime: endTime,
      );
      if (response.isSuccess && response.data?.rows != null) {
        _callRecords.value = response.data!.rows!;
      } else {
        Get.snackbar('Error', response.msg);
      }
    } catch (e) {
      Get.snackbar('Error', e.toString());
    } finally {
      _isLoading.value = false;
    }
  }

  Future<bool> fetchVoiceRecords({
    required int page,
    int size=10,
    String? deviceId,
    DateTime? startTime,
    DateTime? endTime,
    String? voiceFolderId,
  }) async {
    try {
      _isLoading.value = true;
      final response = await _repository.getVoiceRecords(
        page: page,
        size: size,
        voiceType: '3',
        deviceId: deviceId,
        startTime: startTime,
        endTime: endTime,
        voiceFolderId: voiceFolderId,
      );
      if (response.isSuccess && response.data?.rows != null) {
        _voiceRecords.value = response.data!.rows!;
        return true;
      } else {
        Get.snackbar('Error', response.msg);
      }
    } catch (e) {
      Get.snackbar('Error', e.toString());
    } finally {
      _isLoading.value = false;
    }
    return false;
  }

  Future<void> getVoiceRecordDetail(String id) async {
    try {
      _isLoading.value = true;
      final response = await _repository.getVoiceRecordDetail(id);
      if (response.isSuccess && response.data?.data != null) {
        _selectedVoiceRecord.value = response.data?.data;
      } else {
        _selectedVoiceRecord.value = null;
        Get.snackbar('Error', response.msg);
      }
    } catch (e) {
      Get.snackbar('Error', e.toString());
    } finally {
      _isLoading.value = false;
    }
  }

  Future<UploadVoiceEntity?> uploadVoiceRecord(File file) async {
    try {
      _isLoading.value = true;
      final response = await _repository.uploadVoiceRecord(file);
      if (response.isSuccess) {
        Get.snackbar('Success', 'Voice record uploaded successfully');
        return response.data;
      } else {
        Get.snackbar('Error', response.msg);
      }
    } catch (e) {
      Get.snackbar('Error', e.toString());
    } finally {
      _isLoading.value = false;
    }
    return null;
  }
}
